package com.tv.enoguia;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.tv.enoguia.adapter.ListBussinessAdapter;
import com.tv.enoguia.bean.Bussiness;
import com.tv.enoguia.database.MySQLiteHelper;
import com.tv.enoguia.fragment.ListBussinessFragment;

public class ListBussinessActivity extends Fragment implements OnTouchListener{

	TextView title;
	ImageButton btnBack;
	ListView listBus;
	ListBussinessAdapter adapter;
	ArrayList<Bussiness> bussinesses = new ArrayList<Bussiness>();
	MySQLiteHelper db;
	
	String category;
	
	ArrayList<String> idList = new ArrayList<String>();
	
	int liked = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		db = new MySQLiteHelper(getActivity());
		
		category = getActivity().getIntent().getStringExtra("category");
		
		
	}
	
	
	ViewGroup v;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		if(v==null) {
			v = (ViewGroup) inflater.inflate(R.layout.activity_list_bussiness, container, false);
		} else {
			((ViewGroup) v.getParent()).removeView(v);
		}
		
		title = (TextView) v.findViewById(R.id.title);
		btnBack = (ImageButton) v.findViewById(R.id.btn_back);
		btnBack.setVisibility(View.VISIBLE);

		listBus = (ListView) v.findViewById(R.id.list_bussiness);
		addData();
		adapter = new ListBussinessAdapter(getActivity(), bussinesses);
		
		listBus.setAdapter(adapter);
		
		init();
		
		idList = db.getIdLike();
		
		listBus.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				Intent t = new Intent(getActivity(), DetailBussinessActivity.class);
				Log.i("DATA", "id gui di: "+bussinesses.get(position).getId());
				
				for (int i = 0; i < idList.size(); i++) {
					if(bussinesses.get(position).getId().equals(idList.get(i))){
						liked = 1;
					}
				}
				t.putExtra("liked", liked);
				t.putExtra("id", bussinesses.get(position).getId());
				startActivity(t);
			}
			
		});
		
		return v;
	}
	
	public static ListBussinessFragment newInstance() {
		ListBussinessFragment f = new ListBussinessFragment();
	    return f;
	}



	void init() {
		title.setText(category);
		btnBack.setOnTouchListener(this);
	}
	
	void addData(){
		/*bussinesses.add(new Bussiness("Cellar", "Cellar Paramar", "http://goo.gl/0v5NYT"));
		bussinesses.add(new Bussiness("Cellar", "Cellar Paramar", "http://goo.gl/0v5NYT"));
		bussinesses.add(new Bussiness("Cellar", "Cellar Paramar", "http://goo.gl/0v5NYT"));
		bussinesses.add(new Bussiness("Cellar", "Cellar Paramar", "http://goo.gl/0v5NYT"));
		bussinesses.add(new Bussiness("Cellar", "Cellar Paramar", "http://goo.gl/0v5NYT"));*/
		
		bussinesses = db.getBussinessCellar(category);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (v.getId() == R.id.btn_back){
			if (event.getAction() == MotionEvent.ACTION_UP){
			}
		}
		return false;
	}
	
}
